import json
import datetime
import uuid
import requests

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.core import serializers

from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from django.db.models import Q
from django.core.mail import EmailMessage

from django.forms.models import model_to_dict
import time
import hashlib
import random

from Crypto.PublicKey import RSA
from Crypto.Signature import pkcs1_15
from Crypto.Hash import SHA256
from base64 import b64encode
import re
import redis


def getId() -> str:
    '''
    获取 id
    '''
    return getIdFromUUID(uuid.uuid4())


def getIdFromUUID(uuid: str) -> str:
    '''
    从 uuid 中获取 id
    '''
    return str(uuid).replace("-", "").lower()


def isErrorId(id: str) -> bool:
    id = str(id)
    if not id:
        return True
    if len(id) != 32:
        return True
    regex = r"^[0-9a-f]{8}[0-9a-f]{4}[0-9a-f]{4}[0-9a-f]{4}[0-9a-f]{12}$"
    return not re.match(regex, id)


def BaseResPonse(data: dict) -> HttpResponse:
    return HttpResponse(json.dumps(data), content_type="application/json")


def Response(msg: str = '') -> HttpResponse:
    if not msg:
        msg = ''
    data = {
        'status': '10000',
        'msg': msg,
    }
    return BaseResPonse(data)


def WiteResponse() -> HttpResponse:
    data = {
        'status': '30000',
        'msg': "跟单中",
    }
    return BaseResPonse(data)


def AccountNotFoundResponse() -> HttpResponse:
    data = {
        'status': '20000',
        'msg': "账号未注册",
    }
    return BaseResPonse(data)


def LoginErrorResponse() -> HttpResponse:
    data = {
        'status': '20001',
        'msg': "登录失败",
    }
    return BaseResPonse(data)


def ErrResponse(msg: str = "") -> HttpResponse:
    if not msg:
        msg = ''
    data = {
        'status': '-1',
        'msg': msg,
    }
    return BaseResPonse(data)
